package org.example;


import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.example.segmentation.Range;

public class Configuration {
  private Properties properties;

  public Configuration(String configFilePath) throws IOException {
    properties = new Properties();
    try (FileInputStream fis = new FileInputStream(configFilePath)) {
      properties.load(fis);
    }
  }

  public String getSourceDbUrl() {
    return properties.getProperty("source.db.url");
  }

  public String getSourceDbUsername() {
    return properties.getProperty("source.db.username");
  }

  public String getSourceDbPassword() {
    return properties.getProperty("source.db.password");
  }

  public String getTargetDbUrl() {
    return properties.getProperty("target.db.url");
  }

  public String getTargetDbUsername() {
    return properties.getProperty("target.db.username");
  }

  public String getTargetDbPassword() {
    return properties.getProperty("target.db.password");
  }

  public String getTableName() {
    return properties.getProperty("table.name");
  }

  public String getSegmentField() {
    return properties.getProperty("segment.field");
  }

  public int getSegmentSize() {
    return Integer.parseInt(properties.getProperty("segment.size"));
  }

  // 读取手动指定的范围切分
  public List<Range> getManualRanges() {
    List<Range> manualRanges = new ArrayList<>();
    String rangesStr = properties.getProperty("manual.ranges"); // 格式：start1-end1,start2-end2,...
    if (rangesStr != null && !rangesStr.isEmpty()) {
      String[] ranges = rangesStr.split(",");
      for (String range : ranges) {
        String[] limits = range.split("-");
        String start = limits[0];
        String end = limits[1];
        manualRanges.add(new Range(start, end));
      }
    }
    return manualRanges;
  }
}

